home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / non-obj files / crt0-objenv.c < prev    next >
Encoding:
Text File  |  1993-11-09  |  15.2 KB  |  502 lines  |  [TEXT/KAHL]

  1. ].u.fd)) res = i;
  2.     if ((res == -1) && (fd_tab[fd].flags & O_HANDLE))
  3.         {
  4.         if ((fd_tab[fd].flags & O_CREAT) && (GetHandleSize(fd_tab[fd].u.texthandle) > 1+**fd_tab[fd].u.texthandle))
  5.                 {
  6.                 CreateGlobalDocument(fd_tab[fd].u.texthandle);
  7.                 }
  8.         res = 0;
  9.         }
  10.     else if ((res == -1) && fd_tab[fd].u.fd)
  11.             {
  12.             FCBPBRec pb;
  13.             pb.ioRefNum = fd_tab[fd].u.fd;
  14.             pb.ioCompletion = 0;
  15.             pb.ioVRefNum = ioVRefNum;
  16.             PBCloseSync((ParmBlkPtr)&pb);
  17.             res = pb.ioResult;
  18.             }
  19.     else res = fd_tab[fd].u.fd;
  20.     fd_tab[fd].u.fd = 0;
  21.     fd_tab[fd].flags = 0;
  22.     return res;
  23.     }
  24.  
  25. void closeall(void)
  26.     {
  27.     int i;
  28.     for (i = 0; i < OPEN_MAX; i++)
  29.         {
  30.         if (fd_tab[i].u.fd) close(i);
  31.         }
  32.     }
  33.     
  34. int getpagesize(void)
  35.     {
  36.     mysleep(1);
  37.     return 8192;
  38.     }
  39.     
  40. int isatty(int fd)
  41.     {
  42.     mysleep(1);
  43.     return (fd_tab[fd].flags & O_HANDLE) != 0;
  44.     }
  45.         
  46. int getdtablesize(void)
  47.     {
  48.     mysleep(1);
  49.     return OPEN_MAX;
  50.     }
  51.  
  52. static int macstat(StringPtr path, struct stat *buf, short nVRefNum, long lDirID )
  53.         {
  54.         CInfoPBRec  cPB;
  55.            bzero(buf, sizeof(struct stat));
  56.         cPB.hFileInfo.ioNamePtr = path;
  57.         cPB.hFileInfo.ioVRefNum = nVRefNum;
  58.         cPB.hFileInfo.ioDirID = lDirID;
  59.         cPB.hFileInfo.ioFDirIndex = 0;
  60.  
  61.         if (PBGetCatInfoSync(&cPB))
  62.             {
  63.             return -1;
  64.             }
  65.               
  66.         /* Type of file: directory or regular file + access */
  67.         
  68.         if (cPB.hFileInfo.ioFlAttrib & ioDirMask)
  69.             {
  70.              buf->st_ino = cPB.dirInfo.ioDrDirID;
  71.             buf->st_mode = S_IFDIR;
  72.               }
  73.         else
  74.             {
  75.             int siz = cPB.hFileInfo.ioFlClpSiz;
  76.             if (!siz) siz = 8192;
  77.             buf->st_blksize = siz;
  78.             buf->st_blocks = (cPB.hFileInfo.ioFlPyLen+siz-1) / siz;
  79.             buf->st_uid = cPB.hFileInfo.ioFlFndrInfo.fdCreator;
  80.             buf->st_gid = cPB.hFileInfo.ioFlFndrInfo.fdType;
  81.              buf->st_ino = cPB.hFileInfo.ioFRefNum;
  82.             buf->st_size = cPB.hFileInfo.ioFlLgLen;
  83.              buf->st_flags = cPB.hFileInfo.ioFlFndrInfo.fdFlags;
  84.             buf->st_mode = S_IFREG;
  85.                }
  86.         buf->st_mode |= cPB.hFileInfo.ioFlAttrib & 0x01 ? S_IREAD : (S_IREAD | S_IWRITE);
  87.         /* last access time, modification time and creation time(?) */
  88.         buf->st_atime = buf->st_mtime = unixTime(cPB.hFileInfo.ioFlMdDat);
  89.         buf->st_ctime = unixTime(cPB.hFileInfo.ioFlCrDat);
  90.         buf->st_dev = (long)cPB.hFileInfo.ioVRefNum;
  91.         buf->st_nlink = 1;
  92.         return 0;
  93.         }
  94.     
  95. int fstat(int fd, struct stat *buf)
  96.     {
  97.     FCBPBRec pb;
  98.     Str255 name;
  99.     int refnum = fd_tab[fd].u.fd;
  100.     if (fd_tab[fd].flags & O_HANDLE)
  101.         {
  102.            bzero(buf, sizeof(struct stat));
  103.         buf->st_mode = S_IFIFO;
  104.         buf->st_blksize = 8192;
  105.         return 0;
  106.         }
  107.     mysleep(1);
  108.     pb.ioRefNum = refnum;
  109.     pb.ioFCBIndx = 0;
  110.     pb.ioCompletion = 0;
  111.     pb.ioVRefNum = ioVRefNum;
  112.     pb.ioNamePtr = (StringPtr) name;
  113.     PBGetFCBInfoSync(&pb);
  114.     return macstat(name, buf, pb.ioFCBVRefNum, pb.ioFCBParID);
  115.     }
  116.  
  117. int stat(const char *name, struct stat *buf)
  118.     {
  119.     FSSpec myspec;
  120.     mysleep(1);
  121.     myspec.name[0] = strlen(name);
  122.     BlockMove(name, &(myspec.name[1]), *name);
  123.     if (SearchForName(&myspec)) /* open buffer */
  124.         {
  125.            bzero(buf, sizeof(struct stat));
  126.         buf->st_mode = S_IFIFO;
  127.         buf->st_blksize = 8192;
  128.         return 0;
  129.         }
  130.     return macstat(myspec.name, buf, myspec.vRefNum, myspec.parID);
  131.     }
  132.  
  133. void __exit(int status)
  134.     {
  135.     closeall();
  136.     ExitToShell();
  137.     }
  138.  
  139. void (*_exit_vec)(int) = __exit;
  140.  
  141. void _exit(int stat)
  142.     {
  143.     _exit_vec(stat);
  144.     }
  145.  
  146. void abort(void)
  147.     {
  148.     exit(1);
  149.     }
  150.  
  151. int dup2(int fd1, int fd2)
  152.     {
  153.     mysleep(1);
  154.     if (fd_tab[fd2].u.fd) close(fd2);
  155.     fd_tab[fd2] = fd_tab[fd1];
  156.     return fd2;
  157.     }
  158.     
  159. int dup(int fd)
  160.     {
  161.     mysleep(1);
  162.     return dup2(fd, next_fd(3));
  163.     }
  164.  
  165. int fcntl(int fd, int cmd, ...)
  166.      {
  167.      int arg;
  168.      va_list args;
  169.      va_start(args, cmd);
  170.      arg = va_arg(args, int);
  171.      mysleep(1);
  172.      switch(cmd)
  173.          {
  174.          case F_DUPFD : return dup2(fd, next_fd(arg)); break;
  175.          }
  176.      }
  177.  
  178. int writev(int fd, const struct iovec *iov, int iovcnt)
  179.     {
  180.     int i;
  181.     for (i = 0; i < iovcnt; i++)
  182.         {
  183.         write(fd, iov[i].iov_base, iov[i].iov_len);
  184.         }
  185.     }
  186.  
  187. static struct rusage r_tmp = {0};
  188. static struct rlimit rl_tmp = {0};
  189.  
  190. int getrusage(int who, struct rusage *rusage)
  191.     {
  192.     unsigned long ticks;
  193.     static oldticks = 0;
  194.     if (!oldticks) oldticks = *((long *)Ticks);
  195.     ticks = *((long *)Ticks)-oldticks;
  196.     r_tmp.ru_utime.tv_usec = (ticks%60)*(1000000L/60);
  197.     r_tmp.ru_utime.tv_sec = ticks/60;
  198.     *rusage = r_tmp;
  199.     mysleep(1);
  200.     return 0;
  201.     }
  202.  
  203. int getrlimit(int resource, struct rlimit *rlp)
  204.      {
  205.      *rlp = rl_tmp;
  206.      mysleep(1);
  207.      return 0;
  208.      }
  209.  
  210. int setrlimit(int resource, const struct rlimit *rlp)
  211.      {
  212.      rl_tmp = *rlp;
  213.      mysleep(1);
  214.      return 0;
  215.      }
  216.  
  217. time_t    time(time_t *temps)
  218.     {
  219.     long time = unixTime(*((long *)TimeLM));
  220.     if (temps) *temps = time;
  221.     mysleep(1);
  222.     return time;
  223.     }
  224.  
  225. /*
  226. DESCRIPTION
  227.      path points to a path name naming a file.   access()  checks
  228.      the named file for accessibility according to mode, which is
  229.      an inclusive or of the following bits:
  230.  
  231.           R_OK           test for read permission
  232.  
  233.           W_OK           test for write permission
  234.  
  235.           X_OK           test for execute or search permission
  236.  
  237.      The following value may also be supplied for mode:
  238.  
  239.           F_OK           test whether the directories leading  to
  240.                          the  file  can  be searched and the file
  241.                          exists.
  242. */
  243.  
  244. int access(const char *path, int mode)
  245.     {
  246.     struct stat acc;
  247.     mysleep(1);
  248.     if (stat(path, &acc)) return -1;
  249.     return ((mode&W_OK) && !(acc.st_mode&S_IFDIR) && !(acc.st_mode&S_IWUSR)) ?-1 : 0;
  250.     }
  251.     
  252. pid_t getpid(void)
  253.     {
  254.     mysleep(1);
  255.     if (!pid) pid = *((long *)TimeLM)&32767;
  256.     return pid;
  257.     }
  258.  
  259. int kill (pid_t kpid, int x)
  260.     {
  261.     mysleep(1);
  262.     if (kpid == getpid()) exit(1);
  263.     }
  264.  
  265. void macgetwd(void)    
  266.    {
  267.     VolumeParam pb;
  268.     OSErr err;
  269.     CInfoPBRec  cPB;
  270.     cPB.dirInfo.ioDrParID = 0;
  271.     pb.ioNamePtr = (StringPtr) defpath;
  272.     PBGetVolSync((ParmBlkPtr)&pb);
  273.     ioVRefNum = pb.ioVRefNum;
  274.     defpath[0] = 0;
  275.     do
  276.         {
  277.         char tmp[256];
  278.         strcpy(tmp,defpath);
  279.         /* get information about dir */
  280.         cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
  281.         cPB.hFileInfo.ioNamePtr = (StringPtr)defpath;
  282.         cPB.hFileInfo.ioVRefNum = 0;
  283.         cPB.hFileInfo.ioFDirIndex = -1;
  284.         cPB.hFileInfo.ioDirID = cPB.dirInfo.ioDrParID;
  285.  
  286.         err = PBGetCatInfoSync(&cPB); 
  287.         strcpy(defpath+1+*defpath,tmp);
  288.         defpath[0] = '/';
  289.         }
  290.     while ( /* (cPB.dirInfo.ioDrParID != 2) && */ !err);
  291.     
  292.     }
  293.  
  294. char *getwd(char *fullname)
  295.     {
  296.     char *idx;
  297.     static char sfullname[256];
  298.     if (!*defpath) macgetwd();
  299.     idx = index(defpath+1, '/');
  300.     if (!idx) strcpy(sfullname, "/");
  301.     else strcpy(sfullname, idx);
  302.     if (fullname) strcpy(fullname, sfullname);
  303.     else fullname = sfullname;
  304.     return fullname;
  305.     }
  306.  
  307. /*   modf() returns the fractional part of value and  stores  the
  308.      integral  part  indirectly  through iptr.  Thus the argument
  309.      value and the returned values modf() and *iptr satisfy
  310.  
  311.           (*iptr + modf) == value
  312.  
  313.      and both results have the same sign as value.   The  defini-
  314.      tion  of modf() varies among UNIX system implementations, so
  315.      avoid modf() in portable code.
  316. */
  317.  
  318. double modf(double value, double *iptr)
  319.     {
  320.     long x = (int) value;
  321.     if (x >= 0)
  322.         {
  323.         if (value < (double)x) x--;
  324.         }
  325.     else
  326.         {
  327.         if (value > (double)x) x++;
  328.         }
  329.     *iptr = x;
  330.     return value - *iptr;
  331.     }
  332.  
  333. int isinf(double x)
  334.     {
  335.     return 0;
  336.     }
  337.     
  338. int isnan(double x)
  339.     {
  340.     return 0;
  341.     }
  342.  
  343. double ldexp(double x, int n)
  344.     {
  345.     while (n-- > 0) x *= 2;
  346.     while (++n < 0) x /= 2;
  347.     return x;
  348.     }
  349.  
  350. off_t lseek(int fd, off_t off, int nMode)
  351.     {
  352.     ParamBlockRec   pbr;
  353.     mysleep(1);
  354.     if (fd_tab[fd].flags & O_HANDLE)
  355.         {
  356.         switch (nMode)
  357.             {
  358.             case SEEK_SET: fd_tab[fd].off = off; break;
  359.             case SEEK_CUR: fd_tab[fd].off += off; break;
  360.             case SEEK_END: fd_tab[fd].off = GetHandleSize(fd_tab[fd].u.texthandle)-(fd_tab[fd].flags & O_RESOURCE ? 244:0)+off; break;
  361.             }
  362.         return fd_tab[fd].off;
  363.         }
  364.     if (nMode == SEEK_SET)
  365.         nMode = fsFromStart;
  366.     else if (nMode == SEEK_CUR)
  367.         nMode = fsFromMark;
  368.     else if (nMode == SEEK_END)
  369.         nMode = fsFromLEOF;
  370.     pbr.ioParam.ioRefNum = fd_tab[fd].u.fd;
  371.     pbr.ioParam.ioPosMode = nMode;
  372.     pbr.ioParam.ioPosOffset = off;
  373.     PBSetFPosSync(&pbr);
  374.     if ((eofErr == pbr.ioParam.ioResult) && (nMode == fsFromStart))
  375.         {
  376.         pbr.ioParam.ioMisc = (Ptr)off;
  377.         PBSetEOFSync(&pbr);
  378.         pbr.ioParam.ioPosMode = nMode;
  379.         pbr.ioParam.ioPosOffset = off;
  380.         PBSetFPosSync(&pbr);
  381.         }
  382.     return pbr.ioParam.ioPosOffset;
  383.     }    
  384.  
  385. static char *default_environ[] = {"LOGNAME=jrrk",NULL};
  386.  
  387. char **environ = default_environ;
  388.  
  389. int _fmode = O_BINARY;
  390.  
  391. jmp_buf exitbuf;
  392.  
  393. void munge_exit(int n)
  394.     {
  395.     longjmp(exitbuf, n);
  396.     }
  397.  
  398. int handle_read(int fd, char *buf, int n)
  399.     {
  400.     unsigned short flags = fd_tab[fd].flags;
  401.     unsigned long off = fd_tab[fd].off+(flags & O_RESOURCE ? 244:0);
  402.     long cnt = GetHandleSize(fd_tab[fd].u.texthandle)-off;
  403.     char *ptr = off+(char *)*fd_tab[fd].u.texthandle;
  404.     if (n > cnt) n = cnt;
  405.     fd_tab[fd].off += n;
  406.     if (flags & O_TEXT)
  407.         {
  408.         unsigned i = n;
  409.         while (i--) 
  410.             {
  411.             *buf = *ptr++;
  412.             if (*buf == '\r') *buf = '\n';
  413.             buf++;
  414.             }
  415.         }
  416.     else BlockMove(ptr,buf,n);
  417.     mysleep(1);
  418.     return n;
  419.     }
  420.  
  421. int handle_write(int fd, char *buf, int n)
  422.     {
  423.     Handle hand = fd_tab[fd].u.texthandle;
  424.     unsigned char namlen = 1+**hand;
  425.     long siz = GetHandleSize(hand)-namlen;
  426.     long off = fd_tab[fd].off;
  427.     if (off == siz)
  428.         PtrAndHand(buf,hand,n);
  429.     else
  430.         {
  431.         if (off+n > siz) SetHandleSize(hand,off+n+namlen);
  432.         BlockMove(buf, off+*hand+namlen, n);
  433.         }
  434.     fd_tab[fd].off += n;
  435.     mysleep(1);
  436.     return n;
  437.     }
  438.  
  439. extern char gOptions[];
  440.     
  441. void munge(int argc, char **argv, int (*cmd)(int,char **), Handle in, short optstr)
  442.     {
  443.     char out[] = {"\pStandard Output"};
  444.     char err[] = {"\pStandard Error"};
  445.     Handle    strList;
  446.     short optidx;
  447.     /* ensure the STR# resource exists    */
  448.     
  449.     strList = GetResource( 'STR#', optstr);
  450.     FailNILRes( strList);
  451.  
  452.     for (optidx = 1; optidx <= *(short*) *strList; optidx += 2)
  453.         {
  454.         Str255    theString;
  455.         unsigned char active = gOptions[(optidx-1)>>1];
  456.         switch(active)
  457.             {
  458.             case 0: case 1: break;
  459.             case 2: active = 0; break;
  460.             default: active += active-5; break;
  461.             }
  462.         GetIndString(theString,optstr,optidx+active);
  463.         if (!ResError() && theString[0])
  464.             {            
  465.             theString[theString[0]+1] = 0;
  466.             argv[argc++] = strdup((char *)theString+1);
  467.             }
  468.         }
  469.     argv[argc] = 0;
  470.     bzero(fd_tab, sizeof fd_tab);
  471.     fd_tab[0].u.texthandle = in;
  472.     fd_tab[0].flags = O_HANDLE;
  473.     PtrToHand(out,&(fd_tab[1].u.texthandle),1+*out);
  474.     fd_tab[1].flags = O_HANDLE|O_CREAT;
  475.     PtrToHand(err,&(fd_tab[2].u.texthandle),1+*err);
  476.     fd_tab[2].flags = O_HANDLE|O_CREAT;
  477.     _exit_vec = munge_exit;
  478.     mysleep(1);
  479.     TRY
  480.         {
  481.         if (!setjmp(exitbuf))
  482.             exit(cmd(argc, argv));
  483.         }
  484.     CATCH
  485.         {
  486.         _cleanup();
  487.         closeall();
  488.         init_alloca();
  489.         malloc_free_all();    
  490.         }
  491.     ENDTRY;
  492.     _cleanup();
  493.     closeall();
  494.     init_alloca();
  495.     malloc_free_all();
  496.     }
  497.  
  498. int unlink(const char *name)
  499.     {
  500.     return -1;
  501.     }
  502.